package com.sansi.pencilbook.mapper;

import com.sansi.pencilbook.entity.Address;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * 功能说明: 收货地址的增删改查
 *
 * @author lsy13
 */
@Mapper
public interface AddressMapper {

    @Select("select address_id, user_id, store_id, address, phone, addressee from shipping_address")
    public List<Address> findAll() throws SQLException;

    @Select("select address_id, user_id, store_id, address, phone, addressee from shipping_address where user_id=#{id} and store_id=0")
    public List<Address> findByUserId(int id) throws SQLException;

    @Select("select address_id, user_id, store_id, address, phone, addressee " +
            "from shipping_address where user_id=#{id} and address=#{address} and store_id=0")
    public Address findByUserIdAndAddress(int id, String address) throws SQLException;

    @Select("select address_id, user_id, store_id, address, phone, addressee " +
            "from shipping_address where store_id=#{id}")
    public Address findByStoreId(int id) throws SQLException;

    @Insert("insert into shipping_address (user_id, store_id, address, phone, addressee) " +
            "values(#{userId}, #{storeId}, #{address}, #{phone}, #{addressee})")
    public int saveAddress(Address address) throws SQLException;

    @Update("update shipping_address set user_id=#{userId}, " +
            "store_id=#{storeId}, address=#{address}, phone=#{phone}, addressee=#{addressee} where address_id=#{addressId}")
    public int updateAddress(Address address) throws SQLException;

    @Delete("delete from shipping_address where address_id=#{addressId}")
    public int deleteAddress(Address address) throws SQLException;

    @Delete("<script>" +
            "delete from shipping_address where address_id in " +
            "<foreach collection='addresses' item='address' index='index' open='(' separator=',' close=')'>" +
            "#{address.addressId}" +
            "</foreach>" +
            "</script>")
    public int deleteAddresses(List<Address> addresses) throws SQLException;
}
